package com.wang.exam.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.common.pojo.BasePojoEnum;
import com.wang.common.util.BeanUtil;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.common.util.DateUtil;
import com.wang.exam.mapper.AtExamIntvigMapper;
import com.wang.exam.pojo.AtExamDesc;
import com.wang.exam.pojo.AtExamDescEnum;
import com.wang.exam.pojo.AtExamDescQuery;
import com.wang.exam.pojo.AtExamIntvig;
import com.wang.exam.pojo.AtExamIntvigEnum;
import com.wang.exam.pojo.AtExamIntvigQuery;
import com.wang.exam.pojo.AtNotice;
import com.wang.exam.service.AtExamDescService;
import com.wang.exam.service.AtExamIntvigService;
import com.wang.exam.service.AtNoticeService;
import com.wang.users.pojo.AtUsersDesc;
import com.wang.users.pojo.AtUsersDescQuery;
import com.wang.users.service.AtUsersDescService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 针对表【监考记录表】的数据库操作Service实现
 *
 * @author ZjxMi
 */
@Service
@Log4j2
public class AtExamIntvigServiceImpl extends ServiceImpl<AtExamIntvigMapper, AtExamIntvig>
        implements AtExamIntvigService {

    @Autowired
    private BeanUtil beanUtil;
    @Autowired
    private AtUsersDescService usersDescService;
    @Autowired
    private AtExamDescService examDescService;
    @Autowired
    private AtNoticeService noticeService;
    @Autowired
    private DateUtil dateUtil;

    /**
     * model2: jsp --> servlet -->service --> dao --> db, 我们自己封装了一个 PageInfoUtil;
     * MybatisPlus: 不再需要调用 PageInfoUtil,老代码改造的时候是为了整合代码,如果没有老代码,直接写的新代码,采用下面的方式来搞
     *
     * @param page
     * @param query 查询对象
     * @return
     */
    @Override
    public List<AtExamIntvig> findList(IPage<AtExamIntvig> page, AtExamIntvigQuery query) {
        if (page != null) {
            return this.baseMapper.findList(page, query);
        }
        return this.baseMapper.findList(query);
    }

    @Override
    public AtExamIntvig findOne(AtExamIntvigQuery query) {
        return this.baseMapper.findOne(query);
    }

    @Override
    public int updateBatch(AtExamIntvigQuery query) {
        return this.baseMapper.updateBatch(query);
    }

    @Override
    public int deleteBatch(AtExamIntvigQuery query) {
        return this.baseMapper.deleteBatch(query);
    }

    /**
     * 对数据额外处理(非用户填写的数据)
     * @param entity
     */
    private void proccedUpdateData(AtExamIntvig entity){
        if (Objects.isNull(entity.getCreateTime())) {
            entity.setCreateTime(new Date());
        }
        entity.setUpdateTime(new Date());
        /* 判断对象是否为空 */
        if (Objects.isNull(entity.getPubTime())) {
            entity.setPubTime(new Date());
        }
        /* 组装名称和内容 */
        if (Objects.nonNull(entity.getUsersId()) && Objects.nonNull(entity.getExamId())) {
            StringBuilder sbName = new StringBuilder();
            /* 查询用户和考试信息 */
            AtUsersDescQuery usersDescQuery = new AtUsersDescQuery();
            AtUsersDesc usersDescEntity = usersDescQuery.getEntity();
            usersDescEntity.setId(entity.getUsersId());
            AtUsersDesc usersDesc = this.usersDescService.findOne(usersDescQuery);
            if (Objects.nonNull(usersDesc)) {
                sbName.append(usersDesc.getName());
            }
            sbName.append("在" + this.dateUtil.dateToStr(entity.getCreateTime()) + " 申请的监考申请:");
            if (Objects.nonNull(entity.getExamId())) {
                AtExamDescQuery examDescQuery = new AtExamDescQuery();
                AtExamDesc examDescEntity = examDescQuery.getEntity();
                examDescEntity.setId(entity.getExamId());
                AtExamDesc examDesc = this.examDescService.findOne(examDescQuery);
                if (Objects.nonNull(examDesc)) {
                    sbName.append(",考试:" + examDesc.getName());
                }
            }
            entity.setName(sbName.toString());
            entity.setContent(sbName.toString());
        }

        /* 状态为审核通过 */
        if (AtExamDescEnum.STATUSNEW_PASS.getCode().equalsIgnoreCase(entity.getStatus())) {
            /* 生成公告信息 */
            AtNotice notice = new AtNotice();
            notice.setName(entity.getName());
            notice.setContent(entity.getContent());
            boolean dbFlag = this.noticeService.save(notice);
            log.debug("==保存公告信息:结果{}==", dbFlag);
        }
    }

    @Override
    public boolean save(AtExamIntvig entity) {
        /* 判断重复 */
        AtExamIntvigQuery intvigQuery = new AtExamIntvigQuery();
        AtExamIntvig intvigEntity = intvigQuery.getEntity();
        intvigEntity.setUsersId(entity.getUsersId());
        intvigEntity.setExamId(entity.getExamId());
        IPage<AtExamIntvig> intvigPage = new Page<>(1, ConstatFinalUtil.SECOND);
        List<AtExamIntvig> intvigList = this.findList(intvigPage, intvigQuery);
        Assert.isTrue(intvigPage.getTotal() == 0, "监考申请已存在");

        /* 查询监考人数 */
        intvigQuery = new AtExamIntvigQuery();
        intvigEntity = intvigQuery.getEntity();
        intvigEntity.setExamId(entity.getExamId());
        intvigPage = new Page<>(1, ConstatFinalUtil.SECOND);
        intvigList = this.findList(intvigPage, intvigQuery);
        /* 查询考试信息 */
        AtExamDescQuery examDescQuery = new AtExamDescQuery();
        AtExamDesc examDescEntity = examDescQuery.getEntity();
        examDescEntity.setId(entity.getExamId());
        AtExamDesc examDescRes = this.examDescService.findOne(examDescQuery);
        Assert.isTrue(intvigPage.getTotal() < examDescRes.getIntvigNum(), "监考人数已满,无法申请");
        Assert.isTrue(examDescRes.getCurrDate().after(new Date()), "考试时间已过,无法申请");

        this.proccedUpdateData(entity);
        /* 新增加的记录默认未删除 */
        entity.setDelFlag(BasePojoEnum.DELFLAG_NO.getCode());
        entity.setStatus(AtExamIntvigEnum.STATUSNEW_INPUT.getCode());
        return super.save(entity);
    }

    @Override
    public boolean updateById(AtExamIntvig entity) {
        /* 先根据id查询数据库的内容; */
        AtExamIntvigQuery paramQuery = new AtExamIntvigQuery();
        AtExamIntvig entityParam = paramQuery.getEntity();
        entityParam.setId(entity.getId());
        /* 数据库中查询的结果 */
        AtExamIntvig oneDbResult = this.findOne(paramQuery);
        /*
        * 源对象和目标对象是一个类型(10个字段)
        * 源对象有5个字段有值,目标对象是10个有值
        * 把源对象中的5个字段有值,赋值到目标对象中.(覆盖)
        * */
        oneDbResult = (AtExamIntvig) this.beanUtil.copy(entity, oneDbResult);

        this.proccedUpdateData(oneDbResult);
        return super.updateById(oneDbResult);
    }
}